package com.jiayou;

import com.jiayou.Bullet.Bullet1;
import com.jiayou.Bullet.Bullet2;
import com.jiayou.Map.*;
import com.jiayou.Tank.Model;
import com.jiayou.Tank.Tank1;
import com.jiayou.Tank.Tank2;
import com.jiayou.tools.Constclass;
import com.jiayou.tools.TheGameLoading;

import javax.swing.*;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class MyFrame extends Frame {
    private int a = Constclass.HEIGHT;
    public static boolean S = true;
    private static Image[] images = new Image[16];
    static {
        for (int a =1;a<=16;a++)
        {
            images[a-1] = TheGameLoading.getImage(a+".png");
        }
    }
    private List<Bullet2> list = new ArrayList<>();
    private List<Body> bodyList = new ArrayList<>();
    private List<Model> modelList = new ArrayList<>();
    private int[][] map  = new int[36][19];
    private Image  image = TheGameLoading.getImage("bjt.png");
    private Tank1  tank   = new Tank1((Constclass.WEIGHT-400)/2,Constclass.HEIGHT-40,35,35,2,TheGameLoading.getImage("player/player_up.png"));
    public MyFrame() throws HeadlessException {
        super();
        initall();
        this.setTitle("坦克大战");
        this.setSize(Constclass.WEIGHT,Constclass.HEIGHT);
        this.setLocation(Constclass.getwight()/2-Constclass.WEIGHT/2,Constclass.getHeight()/2-Constclass.HEIGHT/2);
        this.setVisible(true);
        this.setResizable(false);
        addKeyListener(new keylistener());
        addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent arg0) {
                System.exit(10);
            }
        });
        new MyThread().start();
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(image,0,0,null);
        Color color = g.getColor();
        g.setColor(Color.white);
        g.setColor(color);
        tank.draw(g);

        for (Model model:modelList)
        {
            model.draw(g,bodyList,tank);
        }
        huazidan(g);
        for (Body body :bodyList)
        {
                body.draw(g,tank);
                body.drawdiren(modelList);
        }
        if(list.size()!=0)
        {
            Bullet2 bullet1 = list.get(0);
            if(bullet1.x<=0 || bullet1.x>=Constclass.WEIGHT || bullet1.y>=Constclass.HEIGHT || bullet1.y<=0)
            {
                list.remove(bullet1);
            }
        }


        GameOver(g);

    }

    private void boom(Graphics graphics,int x,int y)
    {
        for (int a =0;a<=140;a+=1)
        {
            int n = a/10;
                switch (n)
                {
                    case 0:
                        graphics.drawImage(images[0],x,y,40,40,null);
                        break;
                    case 1:
                        graphics.drawImage(images[1],x,y,40,40,null);
                        break;
                    case 2:
                        graphics.drawImage(images[2],x,y,40,40,null);
                        break;
                    case 3:
                        graphics.drawImage(images[3],x,y,40,40,null);
                        break;
                    case 4:
                        graphics.drawImage(images[4],x,y,40,40,null);
                        break;
                    case 5:
                        graphics.drawImage(images[5],x,y,40,40,null);
                        break;
                    case 6:
                        graphics.drawImage(images[6],x,y,40,40,null);
                        break;
                    case 7:
                        graphics.drawImage(images[7],x,y,40,40,null);
                        break;
                    case 8:
                        graphics.drawImage(images[8],x,y,40,40,null);
                        break;
                    case 9:
                        graphics.drawImage(images[9],x,y,40,40,null);
                        break;
                    case 10:
                        graphics.drawImage(images[10],x,y,40,40,null);
                        break;
                    case 11:
                        graphics.drawImage(images[11],x,y,40,40,null);
                        break;
                    case 12:
                        graphics.drawImage(images[12],x,y,40,40,null);
                        break;
                    case 13:
                        graphics.drawImage(images[13],x,y,40,40,null);
                        break;
                    case 14:
                        graphics.drawImage(images[14],x,y,40,40,null);
                        break;
                    case 15:
                        graphics.drawImage(images[15],x,y,40,40,null);
                        break;
                    default:break;

            }

        }


    }

    public static void main(String[] args) {
        new MyFrame();
    }


    //JAVA双重缓冲
    private Image offScreenImage = null;
    public void update(Graphics g) {
        if(offScreenImage == null)
            offScreenImage = this.createImage(Constclass.getwight(),Constclass.getHeight());
        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }
    //刷新帧动画
    public class MyThread extends Thread
    {
        public void run()
        {
            while(true)
            {
                repaint();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }



    //添加对键盘的监听,此时的输入法必须切换为英文，否则监听将不会被触发
    class keylistener implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {

        }
        @Override
        public void keyPressed(KeyEvent e) {
            if (S) {
                switch (e.getKeyCode()) {
                    case 65:  tank.left=true;       break;        //A
                    case 87: tank.up=true;       break;        //W
                    case 68: tank.right=true;     break;        //D
                    case 83: tank.down=true ;	   break;		  //S
                    case 75:
                        switch (tank.sw)
                        {
                            case Constclass.DOWN:
                                if(list.size()==0){
                                    list.add(new Bullet2(tank.x,tank.y,15,Constclass.DOWN));
                                }
                                break;
                            case Constclass.UP:
                                if(list.size()==0)
                                    list.add(new Bullet2(tank.x,tank.y,15,Constclass.UP));
                                break;
                            case Constclass.RIGHT:
                                if(list.size()==0)
                                    list.add(new Bullet2(tank.x,tank.y,15,Constclass.RIGHT));
                                break;
                            case Constclass.LEFT:
                                if(list.size()==0)
                                    list.add(new Bullet2(tank.x,tank.y,15,Constclass.LEFT));
                                break;
                        }
                        break;
                    case 76:
                        if(S)
                        {
                            tank.speed = 3;
                        }
                        break;
                    default:break;
                }
            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case 65: tank.left=false;    break;    //A
                case 87: tank.up=false;        break;      //W
                case 68: tank.right=false; 		break;     //D
                case 83: tank.down=false ;	  break;         //S
                case 76:
                    if(S)
                    {
                        tank.speed = 2;
                    }
                    break;
                default:		break;
            }
        }
    }
        private void initMap()
    {
        for (int a = 0;a<19;a++)
        {
            for (int b = 0;b<36;b++)
            {
                if(a>=17)
                {
                    map[b][a] = 6;
                }else
                {
                    map[b][a] = (int)(1+Math.random()*(4-1+1));
//                    map[b][a] = 1;
                }

            }
        }
        map[16][17]=1;
        map[16][18]=1;
        map[15][17]=1;
        map[14][17]=1;
        map[14][18]=1;
        map[15][18] =5;
        int width = 0;
        int heigh = 40;
        for (int c = 0;c<19;c++)
        {
            width=0;
            heigh+=40;
            for (int d=0;d<36;d++)
            {

                    switch (map[d][c])
                    {
                        case 1:
                            bodyList.add(new Zhuan(width,heigh,40,40));
                            break;
                        case 2:
                            bodyList.add(new Water(width,heigh,40,40));
                            break;
                        case 3:
                            bodyList.add(new Bush(width,heigh,40,40));
                            break;
                        case 4:
                            bodyList.add(new Gang(width,heigh,40,40));
                            break;
                        case 5:
                            bodyList.add(new Home(width,heigh,40,40));
                        default:break;

                }
                width+=40;
            }
        }
    }

    private void initMap1()
    {
        for (int a = 0;a<19;a++)
        {
            for (int b = 0;b<36;b++)
            {
                if(a>=17)
                {
                    map[b][a] = 6;
                }
                else
                  map[b][a] = 1;


            }
        }
        int width = 0;
        int heigh = 40;
        for (int c = 0;c<19;c++)
        {
            width=0;
            heigh+=40;
            for (int d=0;d<36;d++)
            {

                switch (map[d][c])
                {
                    case 1:
                        bodyList.add(new Zhuan(width,heigh,40,40));
                        break;
                    case 2:
                        bodyList.add(new Water(width,heigh,40,40));
                        break;
                    case 3:
                        bodyList.add(new Bush(width,heigh,40,40));
                        break;
                    case 4:
                        bodyList.add(new Gang(width,heigh,40,40));
                        break;
                    case 5:
                        bodyList.add(new Home(width,heigh,40,40));
                    default:break;

                }
                width+=40;
            }
        }
    }

    private void initMap2()
    {
        for (int a = 0;a<19;a++)
        {
            for (int b = 0;b<36;b++)
            {
                if(a>=17)
                {
                    map[b][a] = 6;
                }else
                {
                    map[b][a] = 2;
//                    map[b][a] = 1;
                }

            }
        }

        int width = 0;
        int heigh = 40;
        for (int c = 0;c<19;c++)
        {
            width=0;
            heigh+=40;
            for (int d=0;d<36;d++)
            {

                switch (map[d][c])
                {
                    case 1:
                        bodyList.add(new Zhuan(width,heigh,40,40));
                        break;
                    case 2:
                        bodyList.add(new Water(width,heigh,40,40));
                        break;
                    case 3:
                        bodyList.add(new Bush(width,heigh,40,40));
                        break;
                    case 4:
                        bodyList.add(new Gang(width,heigh,40,40));
                        break;
                    case 5:
                        bodyList.add(new Home(width,heigh,40,40));
                    default:break;

                }
                width+=40;
            }
        }
    }
    private void initMap3()
    {
        for (int a = 0;a<19;a++)
        {
            for (int b = 0;b<36;b++)
            {
                if(a>=17)
                {
                    map[b][a] = 6;
                }else
                {
                    map[b][a] = (int)(1+Math.random()*(4-1+1))>=8 ? 3:(int)(1+Math.random()*(4-1+1));

                }

            }
        }
        map[16][17]=1;
        map[16][18]=1;
        map[15][17]=1;
        map[14][17]=1;
        map[14][18]=1;
        map[15][18] =5;
        int width = 0;
        int heigh = 40;
        for (int c = 0;c<19;c++)
        {
            width=0;
            heigh+=40;
            for (int d=0;d<36;d++)
            {

                switch (map[d][c])
                {
                    case 1:
                        bodyList.add(new Zhuan(width,heigh,40,40));
                        break;
                    case 2:
                        bodyList.add(new Water(width,heigh,40,40));
                        break;
                    case 3:
                        bodyList.add(new Bush(width,heigh,40,40));
                        break;
                    case 4:
                        bodyList.add(new Gang(width,heigh,40,40));
                        break;
                    case 5:
                        bodyList.add(new Home(width,heigh,40,40));
                    default:break;

                }
                width+=40;
            }
        }
    }
    private void initMap4()
    {
        for (int a = 0;a<19;a++)
        {
            for (int b = 0;b<36;b++)
            {
                if(a>=17)
                {
                    map[b][a] = 6;
                }else
                {
                    map[b][a] = (int)(1+Math.random()*(10-1+1))>=5 ? 1:(int)(1+Math.random()*(4-1+1));
                }

            }
        }
        map[16][17]=1;
        map[16][18]=1;
        map[15][17]=1;
        map[14][17]=1;
        map[14][18]=1;
        map[15][18] =5;
        int width = 0;
        int heigh = 40;
        for (int c = 0;c<19;c++)
        {
            width=0;
            heigh+=40;
            for (int d=0;d<36;d++)
            {

                switch (map[d][c])
                {
                    case 1:
                        bodyList.add(new Zhuan(width,heigh,40,40));
                        break;
                    case 2:
                        bodyList.add(new Water(width,heigh,40,40));
                        break;
                    case 3:
                        bodyList.add(new Bush(width,heigh,40,40));
                        break;
                    case 4:
                        bodyList.add(new Gang(width,heigh,40,40));
                        break;
                    case 5:
                        bodyList.add(new Home(width,heigh,40,40));
                    default:break;

                }
                width+=40;
            }
        }
    }
    private void initMap5()
    {
        for (int a = 0;a<19;a++)
        {
            for (int b = 0;b<36;b++)
            {

                map[b][a] = 6;


            }
        }
        map[16][17]=1;
        map[16][18]=1;
        map[15][17]=1;
        map[14][17]=1;
        map[14][18]=1;
        map[15][18] =5;
        int width = 0;
        int heigh = 40;
        for (int c = 0;c<19;c++)
        {
            width=0;
            heigh+=40;
            for (int d=0;d<36;d++)
            {

                switch (map[d][c])
                {
                    case 1:
                        bodyList.add(new Zhuan(width,heigh,40,40));
                        break;
                    case 2:
                        bodyList.add(new Water(width,heigh,40,40));
                        break;
                    case 3:
                        bodyList.add(new Bush(width,heigh,40,40));
                        break;
                    case 4:
                        bodyList.add(new Gang(width,heigh,40,40));
                        break;
                    case 5:
                        bodyList.add(new Home(width,heigh,40,40));
                    default:break;

                }
                width+=40;
            }
        }
    }
    private void initMap6()
    {
        for (int a = 0;a<19;a++)
        {
            for (int b = 0;b<36;b++)
            {

                    map[b][a] = 2;


            }
        }

        int width = 0;
        int heigh = 40;
        for (int c = 0;c<19;c++)
        {
            width=0;
            heigh+=40;
            for (int d=0;d<36;d++)
            {

                switch (map[d][c])
                {
                    case 1:
                        bodyList.add(new Zhuan(width,heigh,40,40));
                        break;
                    case 2:
                        bodyList.add(new Water(width,heigh,40,40));
                        break;
                    case 3:
                        bodyList.add(new Bush(width,heigh,40,40));
                        break;
                    case 4:
                        bodyList.add(new Gang(width,heigh,40,40));
                        break;
                    case 5:
                        bodyList.add(new Home(width,heigh,40,40));
                    default:break;

                }
                width+=40;
            }
        }
    }

    public void initdiren()
    {
        for (int a =0;a<=350;a++)
        {
            modelList.add(new Tank2((Constclass.WEIGHT-400)/2,35,35,40,2,200));
        }
    }

    public void initdiren1()
    {
        for (int a =0;a<=300;a++)
        {
            modelList.add(new Tank2((Constclass.WEIGHT-400)/2,35,35,40,2,300));
        }
    }

    public void initdiren2()
    {
        for (int a =0;a<=100;a++)
        {
            modelList.add(new Tank2((Constclass.WEIGHT-400)/2,35,35,40,2,400));
        }
    }

    public void initdiren3()
    {
        for (int a =0;a<=35;a++)
        {
            modelList.add(new Tank2((Constclass.WEIGHT-400)/2,35,35,40,2,10));
        }
    }

    public void initdiren4()
    {
        for (int a =0;a<=20;a++)
        {
            modelList.add(new Tank2((Constclass.WEIGHT-400)/2-100,40,35,35,2,10));
        }
    }
    public void huazidan(Graphics g)
    {
        boolean b = false;
        if(list.size()!=0)
        {
            b =list.get(0).draw(g,modelList);
            for (Body body:bodyList)
            {
                if(body.rec().intersects(list.get(0).rec()))
                {
                    if(body instanceof Gang)
                    {
                        list.remove(0);
                    }
                    else if(body instanceof Zhuan)
                    {
                        bodyList.remove(body);
                        list.remove(0);
                        boom(g,body.x,body.y);
                        System.out.println("子弹数："+list.size()+"  " + "地图数："+bodyList.size());
                    }
                    else if(body instanceof Home)
                    {
                        boom(g,body.x,body.y);
                        list.remove(0);
                        ((Home) body).image = TheGameLoading.getImage("over.png");
                        S = false;
                    }
                    break;
                }
            }
                if (b && list.size()!=0)
                {
                    list.remove(0);
                }
        }
    }

    private void GameOver(Graphics g)
    {
        if(!S) {
            if (bodyList.size()>0)
                 bodyList.clear();

            if(a<=300)
            {
                g.drawImage(TheGameLoading.getImage("gameover.png"), Constclass.WEIGHT / 3+100, a,240,155,null);
                new MyFrame();
                S = true;
                this.setVisible(false);
            }
            else
                g.drawImage(TheGameLoading.getImage("gameover.png"), Constclass.WEIGHT / 3+100, a--,240,155,null);
        }
        else if (modelList.size()==0)
        {
            new MyFrame();
            S = true;
            this.setVisible(false);
        }
     }


     private void initall()
     {
         int ditu = (int)(1+Math.random()*(7-1+1));
         int diren = (int)(1+Math.random()*(5-1+1));
         if (ditu ==1)
         {
             initMap();
         }
         else if (ditu==2)
         {
             initMap1();
         }
         else if (ditu==3)
         {
             initMap2();
         }
         else if (ditu==4)
         {
             initMap3();
         }
         else if (ditu==5)
         {
             initMap4();
         }
         else if (ditu==6)
         {
             initMap5();
         }
         else if (ditu==7)
         {
             initMap6();
         }

         if (diren ==1)
         {
             initdiren();
         }
         else if (diren==2)
         {
             initdiren1();
         }
         else if (diren==3)
         {
             initdiren2();
         }
         else if (diren==4)
         {
             initdiren3();
         }
         else if (diren==5)
         {
             initdiren4();
         }
     }
}
